{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: StringConcept Extraction with Examples"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: StringConcept Extraction with Examples\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Document, DocumentLLM, StringConcept, StringExample\n",
        "\n",
        "\n",
        "# Create a Document object from text\n",
        "contract_text = \"\"\"\n",
        "SERVICE AGREEMENT\n",
        "This Service Agreement (the \"Agreement\") is entered into as of January 15, 2025 by and between:\n",
        "XYZ Innovations Inc., a Delaware corporation with offices at 123 Tech Avenue, San Francisco, CA \n",
        "(\"Provider\"), and\n",
        "Omega Enterprises LLC, a New York limited liability company with offices at 456 Business Plaza, \n",
        "New York, NY (\"Customer\").\n",
        "\"\"\"\n",
        "doc = Document(raw_text=contract_text)\n",
        "\n",
        "# Create a StringConcept for extracting parties and their roles\n",
        "parties_concept = StringConcept(\n",
        "    name=\"Contract parties\",\n",
        "    description=\"Names of parties and their roles in the contract\",\n",
        "    examples=[\n",
        "        StringExample(content=\"Acme Corporation (Supplier)\"),\n",
        "        StringExample(content=\"TechGroup Inc. (Client)\"),\n",
        "    ],  # add examples providing additional guidance to the LLM\n",
        ")\n",
        "\n",
        "# Attach the concept to the document\n",
        "doc.add_concepts([parties_concept])\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1-mini\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract the concept from the document\n",
        "parties_concept = llm.extract_concepts_from_document(doc)[0]\n",
        "\n",
        "# Print the extracted parties and their roles\n",
        "print(\"Extracted parties and roles:\")\n",
        "for item in parties_concept.extracted_items:\n",
        "    print(f\"- {item.value}\")\n",
        "\n",
        "# Expected output:\n",
        "# - XYZ Innovations Inc. (Provider)\n",
        "# - Omega Enterprises LLC (Customer)\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}